// 导入数据库db对象
const db = require('../config/db');

// 模块系统对象
// https://luckycola.com.cn/public/dist/#/
module.exports = {
    // 1. 发表评论
    addComment(comment_id, to_user_id, from_user_id, comment_content,article_id) {
        let dt = new Date()
        let comment_create_time = dt.toLocaleDateString()+" " + dt.toLocaleTimeString();
        // 拼接sql语句
        let sql = `insert into c_comment(comment_id,to_user_id,from_user_id,comment_content,article_id,comment_create_time) 
        values('${comment_id}','${to_user_id}','${from_user_id}','${comment_content}','${article_id}','${comment_create_time}')`;
        // 返回发布评论的逻辑
        return new Promise((resolve, reject) => {
            // 执行sql语句
            db.query(sql)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            msg: '发布评论成功。'
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '发布评论失败。',
                            err
                        })
                    }
                )
        })
    },
    // 2. 删除评论
    removeComment(comment_id, from_user_id) {
        return new Promise((resolve, reject) => {
            db.query(`select comment_id from c_comment where comment_id = '${comment_id}' and from_user_id = '${from_user_id}'`)
                .then(
                    rows => {
                        if (rows.length == 1) {
                            // 执行删除数据的sql
                            let sql = `delete from c_comment where comment_id = '${comment_id}' and from_user_id = '${from_user_id}'`
                            db.query(sql)
                                .then(
                                    result => {
                                        resolve({
                                            code: 200,
                                            msg: '删除评论成功。'
                                        })
                                    }
                                )
                                .catch(
                                    error => {
                                        reject({
                                            code: -1,
                                            msg: '删除评论失败。'
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该评论不存在。'
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询评论失败。',
                            err
                        })
                    }
                )
        })
    },
    // 3. 回复评论
    addReply(comment_id,message,user_id) {
        return new Promise((resolve, reject) => {
            db.query(`select * from c_comment where comment_id = '${comment_id}'`)
                .then(
                   async (rows) => {
                        // console.log('xxx:',rows)
                        if (rows.length == 1) {
                            // 判断reply_content是否有值
                            if(rows[0].reply_content == ''){
                                rows[0].reply_content = [];
                            }
                            else {
                                rows[0].reply_content = JSON.parse(rows[0].reply_content)
                            }
                            // push返回值是数组长度
                            rows[0].reply_content.push({
                                user_id, 
                                message
                            });
                            // 格式化字符串
                            let reply_str =  JSON.stringify(rows[0].reply_content);
                            // 执行更新数据的sql
                            let sql = `update c_comment set reply_content = '${reply_str}' where  comment_id= '${comment_id}'`;
                            // console.log('sql:',sql);
                           await db.query(sql)
                                .then(
                                    result => {
                                        resolve({
                                            code: 200,
                                            msg: '添加回复成功。'
                                        })
                                    }
                                )
                                .catch(
                                    error => {
                                        reject({
                                            code: -1,
                                            msg: '添加回复失败。'
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该评论不存在。'
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询评论失败。',
                            err
                        })
                    }
                )
        })
    },
    // 4. 获取当前文章的评论
    getComment(article_id){
        return new Promise((resolve,reject)=>{
            db.query(`select * from c_comment where article_id = '${article_id}'`)
            .then(
                rows=> {
                    resolve({
                        code: 200,
                        msg: '获取评论列表成功。',
                        result: [...rows]
                    })
                }
            )
            .catch(
                err=> {
                    reject({
                        code: -1,
                        msg: '获取评论列表失败。'
                    })
                }
            )
        })
    }
}